package com.yisoo.ubag.product.controller;

import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.yisoo.ubag.product.entity.ConsumerEntity;
import com.yisoo.ubag.product.service.ConsumerService;
import com.yisoo.common.utils.PageUtils;
import com.yisoo.common.utils.R;



/**
 * 
 *
 * @author mmciel
 * @email 761998179@qq.com
 * @date 2021-02-04 20:39:14
 */
@RestController
@RequestMapping("product/consumer")
public class ConsumerController {
    @Autowired
    private ConsumerService consumerService;
    @PostMapping("/reg")
    public R regConsumer(@RequestBody ConsumerEntity consumer){
//        LoginStatusVo loginStatusVo = LoginUserInterceptor.loginUser.get();
        consumer.setCreateTime(new Date());
//        System.out.println(consumer.toString());
        consumerService.save(consumer);
        return R.ok();
    }
    @GetMapping("/reg/check/{consumerid}")
    public R regCheckConsumer(@PathVariable("consumerid")String cid){
       return consumerService.getRegCheckInfo(cid);
    }

    @PostMapping("/reg/sendcode")
    public R regSendcode(@RequestBody ConsumerEntity consumer){
        System.out.println(consumer.getEmail());
        System.out.println(consumer.getConsumerId());
        R r = consumerService.Sendcode(consumer);
//        return consumerService.getRegCheckInfo(cid);
        return R.ok();
    }
    @PostMapping("/reg/checkcode/{cid}")
    public R regCheckcode(@PathVariable("cid")Long cid,@RequestBody String vcode){
        JSONObject  jsonObject = JSONObject.parseObject(vcode);
        Map<String,Object> map = (Map<String,Object>)jsonObject;//    //json对象转Map
        return consumerService.Checkcode(cid,(String)map.get("vcode"));
    }
    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("product:consumer:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = consumerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{consumerId}")
    //@RequiresPermissions("product:consumer:info")
    public R info(@PathVariable("consumerId") Long consumerId){
		ConsumerEntity consumer = consumerService.getById(consumerId);

        return R.ok().put("consumer", consumer);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("product:consumer:save")
    public R save(@RequestBody ConsumerEntity consumer){
		consumerService.save(consumer);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("product:consumer:update")
    public R update(@RequestBody ConsumerEntity consumer){
		consumerService.updateById(consumer);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("product:consumer:delete")
    public R delete(@RequestBody Long[] consumerIds){
		consumerService.removeByIds(Arrays.asList(consumerIds));

        return R.ok();
    }

}
